package com.zw.pm.module.overview.statistical;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.module.integrate.files.FilesModel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 统计 控制层
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/overview/statistical")
public class StatisticalController extends BaseController {

    @Autowired
    private StatisticalService statisticalService;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"overview:statistical"})
    public String project(Model model) {

        // 取得项目总数量
        Integer allProjectCount = statisticalService.projectCountByStatus(new LinkedList<Integer>() {{
            add(2);
            add(3);
        }});
        // 取得项目已完成数量
        Integer completeProjectCount = statisticalService.projectCountByStatus(new LinkedList<Integer>() {{
            add(3);
        }});
        // 取得项目未完成数量
        Integer incompleteProjectCount = statisticalService.projectCountByStatus(new LinkedList<Integer>() {{
            add(2);
        }});
        // 取得未发货项目数量
        Integer noSendProjectCount = statisticalService.noSendProjectCountByYearAndMonth(null, null);

        // 计算年月
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;

        // 按年查询项目记录数
        Integer allProjectCountByYear = statisticalService.projectCountByYearAndMonthStatus(year, null, "2,3");
        Integer completedProjectCountByYear = statisticalService.projectCountByYearAndMonthStatus(year, null, "3");
        Integer notCompleteProjectCountByYear = statisticalService.projectCountByYearAndMonthStatus(year, null, "2");
        Integer noSendProjectCountByYear = statisticalService.noSendProjectCountByYearAndMonth(year, null);

        // 按月查询项目记录数
        Integer allProjectCountByMonth = statisticalService.projectCountByYearAndMonthStatus(year, month, "2,3");
        Integer completedProjectCountByMonth = statisticalService.projectCountByYearAndMonthStatus(year, month, "3");
        Integer notCompleteProjectCountByMonth = statisticalService.projectCountByYearAndMonthStatus(year, month, "2");
        Integer noSendProjectCountByMonth = statisticalService.noSendProjectCountByYearAndMonth(year, month);

        // 图表数据取得
        List<String> xAxis = new ArrayList<>();
        List<Integer> noCompleteProject = new ArrayList<>();
        List<Integer> noCompleteNum = new ArrayList<>();
        Integer maxNoCompleteProject = 0;
        Integer maxNoCompleteNum = 0;

        List<StatisticalModel> statisticalModels = statisticalService.barChartData();
        for (StatisticalModel statisticalModel : statisticalModels) {
            xAxis.add(statisticalModel.getFactoryName());
            noCompleteProject.add(statisticalModel.getNoCompleteProject());
            noCompleteNum.add(statisticalModel.getNoCompleteNum());

            // 分别取两个Y轴的最大值
            if (maxNoCompleteProject.compareTo(statisticalModel.getNoCompleteProject()) == -1) {
                maxNoCompleteProject = statisticalModel.getNoCompleteProject();
            }
            // 分别取两个Y轴的最大值
            if (maxNoCompleteNum.compareTo(statisticalModel.getNoCompleteNum()) == -1) {
                maxNoCompleteNum = statisticalModel.getNoCompleteNum();
            }
        }

        model.addAttribute("xAxis", xAxis.toArray(new String[0]));
        model.addAttribute("noCompleteProject", noCompleteProject.toArray(new Integer[0]));
        model.addAttribute("noCompleteNum", noCompleteNum.toArray(new Integer[0]));
        model.addAttribute("maxNoCompleteProject", maxNoCompleteProject + 1);
        model.addAttribute("maxNoCompleteNum", maxNoCompleteNum + 2800);

        //订单类型
        List<StatisticalModel> statisticalModelList = statisticalService.pieChartData();
        Integer systemBuildingOrderCount = 0;
        Integer reserveLibraryOrderCount = 0;
        Integer financeOrderCount = 0;

        for (StatisticalModel statisticalModel : statisticalModelList) {
            if (statisticalModel.getOrderType().compareTo(1) == 0) {
                systemBuildingOrderCount = statisticalModel.getOrderTypeCount();
            } else if (statisticalModel.getOrderType().compareTo(2) == 0) {
                reserveLibraryOrderCount = statisticalModel.getOrderTypeCount();
            } else if (statisticalModel.getOrderType().compareTo(3) == 0) {
                financeOrderCount = statisticalModel.getOrderTypeCount();
            }
        }
        model.addAttribute("systemBuildingOrderCount", systemBuildingOrderCount);
        model.addAttribute("reserveLibraryOrderCount", reserveLibraryOrderCount);
        model.addAttribute("financeOrderCount", financeOrderCount);

        // 当前年月
        model.addAttribute("year", year);
        model.addAttribute("month", month);

        // 项目总数量
        model.addAttribute("allProjectCount", allProjectCount);
        // 取得项目已完成数量
        model.addAttribute("completeProjectCount", completeProjectCount);
        // 取得项目未完成数量
        model.addAttribute("incompleteProjectCount", incompleteProjectCount);
        // 取得未发货项目数量
        model.addAttribute("noSendProjectCount", noSendProjectCount);

        // 按年查询项目记录数
        model.addAttribute("allProjectCountByYear", allProjectCountByYear);
        model.addAttribute("completedProjectCountByYear", completedProjectCountByYear);
        model.addAttribute("notCompleteProjectCountByYear", notCompleteProjectCountByYear);
        model.addAttribute("noSendProjectCountByYear", noSendProjectCountByYear);

        // 按月查询项目记录数
        model.addAttribute("allProjectCountByMonth", allProjectCountByMonth);
        model.addAttribute("completedProjectCountByMonth", completedProjectCountByMonth);
        model.addAttribute("notCompleteProjectCountByMonth", notCompleteProjectCountByMonth);
        model.addAttribute("noSendProjectCountByMonth", noSendProjectCountByMonth);

        return "overview/statistical/page";
    }

    /**
     * 年查询
     *
     * @param year
     * @return
     */
    @ResponseBody
    @GetMapping("/year/projectCount")
    @RequiresPermissions({"overview:statistical"})
    public JsonResult yearProjectCount(Integer year) {
        StatisticalModel model = new StatisticalModel();
        model.setProjectCount(statisticalService.projectCountByYearAndMonthStatus(year, null, "2,3"));
        model.setProjectCompletedCount(statisticalService.projectCountByYearAndMonthStatus(year, null, "3"));
        model.setProjectNotCompleteCount(statisticalService.projectCountByYearAndMonthStatus(year, null, "2"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(model);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 月查询
     *
     * @param month
     * @return
     */
    @ResponseBody
    @GetMapping("/month/projectCount")
    @RequiresPermissions({"overview:statistical"})
    public JsonResult monthProjectCount(Integer year, Integer month) {
        StatisticalModel model = new StatisticalModel();
        model.setProjectCount(statisticalService.projectCountByYearAndMonthStatus(year, month, "2,3"));
        model.setProjectCompletedCount(statisticalService.projectCountByYearAndMonthStatus(year, month, "3"));
        model.setProjectNotCompleteCount(statisticalService.projectCountByYearAndMonthStatus(year, month, "2"));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(model);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 年查询未发货项目数量
     *
     * @param year
     * @return
     */
    @ResponseBody
    @GetMapping("/year/noSendProjectCount")
    @RequiresPermissions({"overview:statistical"})
    public JsonResult yearNoSendProjectCount(Integer year) {
        StatisticalModel model = new StatisticalModel();
        model.setNoSendProjectCount(statisticalService.noSendProjectCountByYearAndMonth(year, null));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(model);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 月查询未发货项目数量
     *
     * @param year
     * @return
     */
    @ResponseBody
    @GetMapping("/month/noSendProjectCount")
    @RequiresPermissions({"overview:statistical"})
    public JsonResult monthNoSendProjectCount(Integer year, Integer month) {
        StatisticalModel model = new StatisticalModel();
        model.setNoSendProjectCount(statisticalService.noSendProjectCountByYearAndMonth(year, month));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(model);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

}
